<?php

namespace App\Http\Controllers\Api;

use App\Repositories\Api\DailyMenuRepository;
use App\Repositories\Api\MemberRepository;
use App\Repositories\Api\ProductRepository;
use App\Repositories\Api\SupplierRepository;

class ProductController extends MiniAppController
{
    private $memberRep = NULL,
        $productRep = NULL;

    public function __construct(
        MemberRepository  $memberRep,
        ProductRepository $productRep
    )
    {
        parent::__construct();

        $this->memberRep = $memberRep;
        $this->productRep = $productRep;
    }

    public function index()
    {
        $params = request()->all();
        $params['uid'] = UID;
        $res = $this->productRep->getPages($params, $this->pageSize, $this->pageNum);

        return retSuc($res);
    }

    public function detail()
    {
        $id = intval(request()->get("id", 0));
        if (!$id)
            return retFail("参数错误");

        $res = $this->productRep->getOne(intval($id));
        return retSuc([
            'goods' => $res
        ]);
    }

    /**
     * 获取栏目对应产品
     */
    public function cate_list()
    {
        $params = request()->all();
        $res = $this->productRep->getProductByCategoryList($params);

        return retSuc(['data' => $res]);
    }

    /**
     * 菜单产品
     */
    public function menu_productList()
    {
        $shop_id = intval(request()->get("shop_id", 0));
        $menu_id = intval(request()->get("menu_id", 0));

        if (empty($shop_id))
            return retFail("参数错误");

        $shop = (new SupplierRepository())->getOne($shop_id);

        if (empty($shop))
            return retFail("店铺不存在");

        $res = (new DailyMenuRepository())->getMenusProductList(0, $menu_id ?? 0, $shop['id']);
        $res['shop'] = $shop ?? [];
        $res['member'] = (new MemberRepository())->getSimpleMember(UID, 2);

        return retSuc($res);
    }

    /**
     * 套餐菜品列表
     */
    public function combo_productList()
    {
        $combo_id = intval(request()->get("combo_id", 0));

        if (!$combo_id)
            return retFail("参数错误");

        $combo = (new DailyMenuRepository())->getComboById($combo_id);

        if (empty($combo))
            return retFail("套餐不存在或已过期");

        $res = (new DailyMenuRepository())->getComboProductListByComboId($combo_id);

        return retSuc([
            'productList' => $res,
            'combo' => $combo,
            'supplier' => (new SupplierRepository())->getSupplierByUid(UID)
        ]);
    }

}
